﻿using System;
using Excel.Expressions.Compiler;
using Expressions.Compiler;
using Expressions.Compiler.Interfaces;
using Excel.Expressions.Interfaces;

namespace Excel.Expressions
{
    internal partial class Nonterminals__
    {
        private static Rules _Topt()
        {
            return new Rules
            {
                () => '*' + F[false] + Topt[true],
                () => '/' + F[false] + Topt[true],
            };
        }
    }

    internal partial class Expressions__
    {
        private static Func<CellExpression, IExpressionTree, CellExpression> _Topt()
        {
            return (a, b) =>
            {
                if (b[0].Context.Token.Equals('*') && b[1].Context.Token.Equals(Nonterminals.F) && (b[2] == null || b[2].Context.Token.Equals(Nonterminals.Topt)))
                {
                    return Topt[new Mul(a, F[a, b[1]]), b[2]];
                }
                if (b[0].Context.Token.Equals('/') && b[1].Context.Token.Equals(Nonterminals.F) && (b[2] == null || b[2].Context.Token.Equals(Nonterminals.Topt)))
                {
                    return Topt[new Div(a, F[a, b[1]]), b[2]];
                }
                throw new NotSupportedException("Input token is not supported");
            };
        }
    }
}
